﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using yncTech.BusinessObjects;
using yncTech.Common;

public partial class Admin_Sys_Dic_Dic_Company_A : AdminBasePage
{
    string companyCode = string.Empty;
    string companyName = string.Empty;
    public string cat = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString[BFCompanyDicMetadata.ColumnNames.CompanyCode] != null)
        {
            companyCode = Request.QueryString[BFCompanyDicMetadata.ColumnNames.CompanyCode];
        }


        if (!IsPostBack)
        {
            switch (ActionType)
            {
                case OperationType.Add:

                    btnSave.Text = "新增";
                    break;
                case OperationType.Update:

                    BFCompanyDic company = new BFCompanyDic();
                    company.LoadByPrimaryKey(companyCode, companyName);

                    txtCompanyCode.Text = company.CompanyCode;
                    txtCompanyCode.ReadOnly = true;
                    txtCompanyName.Text = company.CompanyName;
                    txtCompanyNo.Text = company.CompanyNo;


                    btnSave.Text = "修改";
                    break;
                case OperationType.Delete:
                    btnSave_Click(null, null);
                    break;
            }
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        lbMessage.Text = "";
        try
        {
            BFCompanyDic company = new BFCompanyDic();

            switch (ActionType)
            {
                case OperationType.Add:
                    company.Query.Where(company.Query.CompanyCode == txtCompanyCode.Text.Trim());
                    company.Query.Load();
                    if (company.es.HasData)
                    {
                        lbMessage.Text = "公司代码已存在！";
                        return;
                    }
                    company.CompanyCode = txtCompanyCode.Text;
                    company.CompanyName = txtCompanyName.Text;
                    company.CompanyNo = txtCompanyNo.Text;
                    company.CreatedBy = company.LastedUpdatedBy = LoginUserInfo.CurrentUser.UserName;
                    company.CreatedDate = company.LastedUpdatedDate = DateTime.Now;
                    company.Save();
                    break;
                case OperationType.Update:
                    company.LoadByPrimaryKey(companyCode, companyName);
                    company.CompanyCode = txtCompanyCode.Text;
                    company.CompanyName = txtCompanyName.Text;
                    company.CompanyNo = txtCompanyNo.Text;
                    company.LastedUpdatedBy = LoginUserInfo.CurrentUser.LoginName;
                    company.LastedUpdatedDate = DateTime.Now;
                    company.Save();
                    break;
                case OperationType.Delete:
                    company.LoadByPrimaryKey(companyCode, companyName);
                    company.MarkAsDeleted();
                    company.Save();
                    WebHelper.GotoParent();
                    break;
            }
            WebHelper.CloseDialog(true);
        }
        catch (Exception ex)
        {
            lbMessage.Text = ex.Message;
        }

    }
}